package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ILawsRegulationsTypeDAO;
import org.jsola.hr.dto.LawsRegulationsTypeAddDTO;
import org.jsola.hr.dto.LawsRegulationsTypeUpdateDTO;
import org.jsola.hr.entity.LawsRegulationsTypeDO;
import org.jsola.hr.query.LawsRegulationsTypeQuery;
import org.jsola.hr.service.ILawsRegulationsTypeService;
import org.jsola.hr.vo.LawsRegulationsTypeListVO;
import org.jsola.hr.vo.LawsRegulationsTypeVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卫人云-政策法规分组
 *
 * @author zhr
 */
@Service("hrLawsRegulationsTypeServiceImpl")
public class LawsRegulationsTypeServiceImpl implements ILawsRegulationsTypeService {

    @Autowired
    private ILawsRegulationsTypeDAO lawsRegulationsTypeDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public LawsRegulationsTypeVO save(LawsRegulationsTypeAddDTO lawsRegulationsTypeAddDTO, TokenUser tokenUser) {
        // 转DO
        LawsRegulationsTypeDO lawsRegulationsTypeDO = lawsRegulationsTypeAddDTO.to(LawsRegulationsTypeDO.class);
        // 保存
        lawsRegulationsTypeDO = save(lawsRegulationsTypeDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return lawsRegulationsTypeDO.to(LawsRegulationsTypeVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(LawsRegulationsTypeUpdateDTO lawsRegulationsTypeUpdateDTO, TokenUser tokenUser) {
        //转DO
        LawsRegulationsTypeDO lawsRegulationsTypeDO = lawsRegulationsTypeUpdateDTO.to(LawsRegulationsTypeDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(lawsRegulationsTypeDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... lawsRegulationsTypeIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) lawsRegulationsTypeIds);
    }


    @Override
    public LawsRegulationsTypeVO selectById(String lawsRegulationsTypeId, String siteId) {
        LawsRegulationsTypeDO lawsRegulationsTypeDO = selectDOById(lawsRegulationsTypeId, siteId);
        if (lawsRegulationsTypeDO == null) {
            return null;
        }
        return lawsRegulationsTypeDO.to(LawsRegulationsTypeVO.class);
    }

    @Override
    public List<LawsRegulationsTypeListVO> select(LawsRegulationsTypeQuery lawsRegulationsTypeQuery, String siteId) {
        List<LawsRegulationsTypeDO> lawsRegulationsTypeDOList = selectDO(lawsRegulationsTypeQuery, siteId);
        if(CollectionUtils.isEmpty(lawsRegulationsTypeDOList)) {
            return lawsRegulationsTypeDOList == null ? null : new ArrayList<>();
        }
        return lawsRegulationsTypeDOList.stream()
                .map(lawsRegulationsTypeDO -> lawsRegulationsTypeDO.to(LawsRegulationsTypeListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(LawsRegulationsTypeQuery lawsRegulationsTypeQuery, String siteId) {
        Example example = buildExample(lawsRegulationsTypeQuery,siteId);
        return lawsRegulationsTypeDAO.selectCountByExample(example);
    }

    @Override
    public Page<LawsRegulationsTypeListVO> selectPage(LawsRegulationsTypeQuery lawsRegulationsTypeQuery, String siteId) {
        Example example = buildExample(lawsRegulationsTypeQuery,siteId);
        Page<LawsRegulationsTypeDO> page = lawsRegulationsTypeDAO.selectPageByExample(example,
                lawsRegulationsTypeQuery.getPageNo(),
                lawsRegulationsTypeQuery.getPageSize());

        return page.to(LawsRegulationsTypeListVO.class);
    }

    @Override
    public LawsRegulationsTypeDO selectDOById(String lawsRegulationsTypeId, String siteId) {
        return listById(lawsRegulationsTypeId, siteId);
    }

    @Override
    public List<LawsRegulationsTypeDO> selectDO(LawsRegulationsTypeQuery lawsRegulationsTypeQuery, String siteId) {
        Example example = buildExample(lawsRegulationsTypeQuery,siteId);
        return lawsRegulationsTypeDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param lawsRegulationsTypeQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(LawsRegulationsTypeQuery lawsRegulationsTypeQuery, String siteId) {
        Example example = new Example(LawsRegulationsTypeDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,lawsRegulationsTypeQuery.getOrders());
        return example;
    }
}




